﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenNETCF.IoC.UI;
using OpenNETCF.IoC;
using MachineSimulator.Presenters;
using System.Threading;
using System.Diagnostics;

namespace MachineSimulator.Views
{
    public partial class MachineView : SmartPart
    {
        MachinePresenter Presenter { get; set; }

        public MachineView()
        {
            InitializeComponent();
        }

        [InjectionConstructor]
        public MachineView([ServiceDependency]MachinePresenter presenter)
            : this()
        {
            this.Name = "Machine";

            Presenter = presenter;
            xValue.DataBindings.Add("Text", Presenter, "XPos");
            yValue.DataBindings.Add("Text", Presenter, "YPos");
            powerValue.DataBindings.Add("Text", Presenter, "Power");

            xSlider.ValueChanged += new EventHandler(xSlider_ValueChanged);
            ySlider.ValueChanged += new EventHandler(ySlider_ValueChanged);
            powerSlider.ValueChanged += new EventHandler(powerSlider_ValueChanged);
            doorOpen.CheckedChanged += new EventHandler(doorOpen_CheckedChanged);
            estop.CheckedChanged += new EventHandler(estop_CheckedChanged);

            m_animationTimer.Tick += new EventHandler(m_animationTimer_Tick);
            m_animationTimer.Interval = 200;
            m_animationTimer.Enabled = true;
        }

        private System.Windows.Forms.Timer m_animationTimer = new System.Windows.Forms.Timer();

        private double m_xscale;
        private double m_yscale;
        private Bitmap m_backBuffer;

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if (machinePanel != null)
            {
                m_xscale = machinePanel.Width / 1000F;
                m_yscale = machinePanel.Height / 1000f;

                if (m_backBuffer != null)
                {
                    m_backBuffer.Dispose();
                }

                m_backBuffer = new Bitmap(machinePanel.Width, machinePanel.Height);
                using (var g = Graphics.FromImage(m_backBuffer))
                using(var brush = new SolidBrush(Color.Black))
                {
                    g.FillRectangle(brush, 0, 0, m_backBuffer.Width, m_backBuffer.Height);
                }
            }
        }

        void estop_CheckedChanged(object sender, EventArgs e)
        {
            Presenter.EStop = estop.Checked;
        }

        void doorOpen_CheckedChanged(object sender, EventArgs e)
        {
            Presenter.DoorOpen = doorOpen.Checked;
        }

        void powerSlider_ValueChanged(object sender, EventArgs e)
        {
            Presenter.Power = powerSlider.Value;
        }

        void ySlider_ValueChanged(object sender, EventArgs e)
        {
            Presenter.YPos = ySlider.Value - 500;
        }

        void xSlider_ValueChanged(object sender, EventArgs e)
        {
            Presenter.XPos = xSlider.Value - 500;
        }

        Graphics m_graphics;
        SolidBrush m_brush;

        void m_animationTimer_Tick(object sender, EventArgs e)
        {
            if (m_graphics == null)
            {
                m_graphics = machinePanel.CreateGraphics();
                m_brush = new SolidBrush(Color.Red);
            }

            m_graphics.DrawImage(m_backBuffer, 0, 0);

            var x = (int)((Presenter.XPos * m_xscale) + machinePanel.Width / 2);
            var y = (int)((Presenter.YPos * m_yscale) + machinePanel.Height / 2);
            m_graphics.FillEllipse(m_brush, x, y, 8, 8);
        }
    }
}
